package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyTransferChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportClientProxyClientConnectionTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportClientProxyClientConnectTransferTypeAdvanced
        extends AbstractHandleSocksReportClientProxyClientConnectionTransferTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel transferChannel = nettyChannelContext.channel();


        String host = nettyProxyMsg.targetIp();
        Integer port = Integer.parseInt(nettyProxyMsg.targetPort());
        byte[] data = nettyProxyMsg.getData();
        byte[] visitorId = nettyProxyMsg.getVisitorId();
        String targetClientId = nettyProxyMsg.clientId();
        String msgVisitorId = new String(visitorId);
        ChannelAttributeKeyUtils.buildClientId(transferChannel, targetClientId);
        ChannelAttributeKeyUtils.buildVisitorId(transferChannel, msgVisitorId);
        ChannelAttributeKeyUtils.buildTargetIp(transferChannel, host);
        ChannelAttributeKeyUtils.buildTargetPort(transferChannel, port);

        NettyTransferChannelContext.pushVisitor(transferChannel, msgVisitorId);

        // 客户端连接客户端传输通道
        Channel loadBalance = ChannelContext.getLoadBalance(targetClientId);
        if (ObjectUtils.isNotEmpty(loadBalance)) {
            // 下发创建新链接指令
            NettyProxyMsg otherClientConnectServer = new NettyProxyMsg();
            otherClientConnectServer.setData(data);
            otherClientConnectServer.setTargetIp(host);
            otherClientConnectServer.setTargetPort(port);
            otherClientConnectServer.setVisitorId(visitorId);
            otherClientConnectServer.setClientId(targetClientId);
            otherClientConnectServer.setType(ProxyMessageType.SOCKS_DISTRIBUTE_CLIENT_PROXY_CLIENT_OTHER_TRANSFER_CONNECTION_);
            loadBalance.writeAndFlush(otherClientConnectServer);

        } else {
            log.error("无法通过客户端ID获取当前客户端通道");
        }


    }

}
